from scipy.optimize import minimize
import numpy as np
# #先想办法计算不同工地距离两个料场的距离。
x_position=np.matrix([1.25,8.75,0.5,5.75,3,7.25])
y_position=np.matrix([1.25,0.75,4.75,5,6.5,7.25])
def myfunction(x):
    x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,=x
    return (((x12-x_position[0,0])**2+(x13-y_position[0,0])**2)**0.5)*x0+(((x14-x_position[0,0])**2+(x15-y_position[0,0])**2)**0.5)*x6+(((x12-x_position[0,1])**2+(x13-y_position[0,1])**2)**0.5)*x1+(((x14-x_position[0,1])**2+(x15-y_position[0,1])**2)**0.5)*x7+(((x12-x_position[0,2])**2+(x13-y_position[0,2])**2)**0.5)*x2+(((x14-x_position[0,2])**2+(x15-y_position[0,2])**2)**0.5)*x8+(((x12-x_position[0,3])**2+(x13-y_position[0,3])**2)**0.5)*x3+(((x14-x_position[0,3])**2+(x15-y_position[0,3])**2)**0.5)*x9+(((x12-x_position[0,4])**2+(x13-y_position[0,4])**2)**0.5)*x4+(((x14-x_position[0,4])**2+(x15-y_position[0,4])**2)**0.5)*x10+(((x12-x_position[0,5])**2+(x13-y_position[0,5])**2)**0.5)*x5+(((x14-x_position[0,5])**2+(x15-y_position[0,5])**2)**0.5)*x11
# obj=lambda x:(((x[12]-x_position[0])**2+(x[13]-y_position[0])**2)**0.5)*x[0]+(((x[14]-x_position[0])**2+(x[15]-y_position[0])**2)**0.5)*x[6]+(((x[12]-x_position[1])**2+(x[13]-y_position[1])**2)**0.5)*x[1]+(((x[14]-x_position[1])**2+(x[15]-y_position[1])**2)**0.5)*x[7]+(((x[12]-x_position[2])**2+(x[13]-y_position[2])**2)**0.5)*x[2]+(((x[14]-x_position[2])**2+(x[15]-y_position[2])**2)**0.5)*x[8]+(((x[12]-x_position[3])**2+(x[13]-y_position[3])**2)**0.5)*x[3]+(((x[14]-x_position[3])**2+(x[15]-y_position[3])**2)**0.5)*x[9]+(((x[12]-x_position[4])**2+(x[13]-y_position[4])**2)**0.5)*x[4]+(((x[14]-x_position[4])**2+(x[15]-y_position[4])**2)**0.5)*x[10]+(((x[12]-x_position[5])**2+(x[13]-y_position[5])**2)**0.5)*x[5]+(((x[14]-x_position[5])**2+(x[15]-y_position[5])**2)**0.5)*x[11]
cons=[{'type':'eq','fun':lambda x:x[0]+x[6]-3},
      {'type':'eq','fun':lambda x:x[1]+x[7]-5},
      {'type':'eq','fun':lambda x:x[2]+x[8]-4},
      {'type':'eq','fun':lambda x:x[3]+x[9]-7},
      {'type':'eq','fun':lambda x:x[4]+x[10]-6},
      {'type':'eq','fun':lambda x:x[5]+x[11]-11},
      {'type':'ineq','fun':lambda x:-x[0]-x[1]-x[2]-x[3]-x[4]-x[5]+20},
      {'type':'ineq','fun':lambda x:-x[6]-x[7]-x[8]-x[9]-x[10]-x[11]+20}]


boundry=[(0,20),(0,20),(0,20),(0,20),(0,20),(0,20),(0,20),(0,20),(0,20),(0,20),(0,20),(0,20),(0,20),(0,20),(0,20),(0,20)]
initial_number=[3, 5, 0, 7,0, 1, 0, 0,4,0, 6, 10,5,1,2,7]
res=minimize(myfunction,initial_number,bounds=boundry,method='L-BFGS-B')
# res=minimize(obj,initial_number,constraints=cons,bounds=boundry)

print(res.fun)#最小值
print(res.success)#求解状态
print(res.x)#最小值对应自变量取值
# print(x_position[0,0])















